<?php
	/**
	 * @package AmpleHarvest
	 */
	require_once 'TableHeader.php';

	/**
	 * @brief This class Describes the Query_History Table in the AmpleHarvest Database
	 *
	 * - Revision History
	 *  - 0.1
	 * 	 - <b>Date</b>: 07 NOV 2011
	 * 	 - <b>Author</b>: Randy Blancett
	 * 	  - Initial Version
	 *
	 * @author Randy Blancett
	 * @version 0.1
	 * @ingroup code classes table
	 */
	class cTABLE_GroupTags extends cTable
	{

		/**
		 * @{
		 * @name Columns
		 */
		public $m_int_COL_ID = null;
		public $m_int_COL_GroupID = null;
		public $m_int_COL_Tag = null;
		public $m_int_COL_Creator = null;
		public $m_int_COL_Date = null;
		/**
		 * @}
		 */

		/**
		 * This constructor sets all the required column data
		 */
		public function __construct()
		{
			$this->m_str_TableName = "group_tags";
			$this->set_Engine(self::C_STR_ENGINE_INNODB);

			//Create Column Objects
			$obj_COL_ID = new cColVarChar("id");
			$obj_COL_ID->set_Length(36);
			$obj_COL_ID->set_Index(abs_Column::c_int_INDEX_PRIMARY);
			$obj_COL_ID->set_Comment("This column holds the ID of the referance.");
			$obj_COL_ID->set_Editable(false);
			$obj_COL_ID->set_Label("ID");

			$obj_COL_Group_ID = new cColVarChar("group_id");
			$obj_COL_Group_ID->set_Length(36);
			$obj_COL_Group_ID->set_Index(abs_Column::c_int_INDEX_INDEX);
			$obj_COL_Group_ID->set_Comment("This column holds the ID of the Group the option is for.");
			$obj_COL_Group_ID->set_Editable(false);
			$obj_COL_Group_ID->set_Label("Group ID");

			$obj_COL_Tag = new cColVarChar("tag");
			$obj_COL_Tag->set_Length(200);
			$obj_COL_Tag->set_Index(abs_Column::c_int_INDEX_INDEX);
			$obj_COL_Tag->set_Comment("This column holds the Tag name.");
			$obj_COL_Tag->set_Label("Tag");

			$obj_COL_Date = new cColDateTime("date");
			$obj_COL_Date->set_Index(abs_Column::c_int_INDEX_INDEX);
			$obj_COL_Date->set_Comment("This column holds the date the data was added.");
			$obj_COL_Date->set_Label("Creation Date");

			$obj_COL_Creator = new cColVarChar("creator");
			$obj_COL_Creator->set_Length(100);
			$obj_COL_Creator->set_Index(abs_Column::c_int_INDEX_INDEX);
			$obj_COL_Creator->set_Comment("This column holds the creator of the tag.");
			$obj_COL_Creator->set_Label("Creator");

			//Set up connection
			$this->m_obj_Connection = new cConnection("AmpleHarvest", "admin");

			//Activate all Debug options
			$this->m_obj_Connection->set_DebugLevel(cLog_Connection_Debug::c_int_LEVEL_HIDDEN);
			$this->m_obj_Connection->set_LogLevel(cLog_Connection::c_int_LEVEL_BASIC);
			//$this->m_obj_Connection->activate_Logging();
			//$this->m_obj_Connection->activate_LogDebug();
			//$this->m_obj_Connection->activate_ScreenDebug();

			$this->m_int_COL_ID = $this->add_Column($obj_COL_ID);
			$this->m_int_COL_GroupID = $this->add_Column($obj_COL_Group_ID);
			$this->m_int_COL_Tag = $this->add_Column($obj_COL_Tag);
			$this->m_int_COL_Creator = $this->add_Column($obj_COL_Creator);
			$this->m_int_COL_Date = $this->add_Column($obj_COL_Date);
		}

		/**
		 * This function is used to insert Data into the database
		 */
		public function insert_Data2DB()
		{
			$this->set_id($this->create_GUID());

			return parent::insert_Data2DB();
		}

		/**
		 * Saves Information to the Database
		 */
		public function save_Data2DB()
		{
			if ($this->validate_ID($this->get_id()))
			{
				return $this->update_Data2DB();
			}
			else
			{
				return $this->insert_Data2DB();
			}
		}

		public function insert_DefaultData()
		{
			return true;
		}

		/**
		 * Returns if the id is a valid and existing ID
		 * @param string $str_User_ID ID of the Page to check (GUID)
		 * @return boolean true if valid false if not
		 */
		public function validate_ID($str_ID)
		{
			// ensure that there is an ID passed and that it is a valid GUID
			if (!empty($str_ID) && preg_match(self::c_REG_GUID_VALID, $str_ID))
			{
				$this->clear_Query();
				$this->add_ColumnQuery($this->m_int_COL_ID, $str_ID);
				if ($this->do_Search() > 0)
				{
					return true;
				}
			}
			return false;
		}

		/**
		 * Loads information into object from Database based on ID passed
		 * @param string $str_ID ID of the id to check (int)
		 * return boolean true if loaded false if not
		 */
		public function load_DataFromID($str_ID)
		{
			$this->clear();

			if ($this->validate_ID($str_ID))
			{
				$this->clear_Query();
				$this->add_ColumnQuery($this->m_int_COL_ID, $str_ID);
				if ($this->do_Search() > 0)
				{
					$obj_RS = $this->get_ResultSet();
					if ($obj_RS[0])
					{
						$this->load_DataFromArray($obj_RS[0]);
						return true;
					}
				}
			}
			return false;
		}

		public function get_Tags4Group($str_GroupID, $int_SortColumn = -1, $str_SortColumn = cConnection::c_str_SORT_ASC)
		{
			$arr_Output = array();
			if ($int_SortColumn < 0)
			{
				$int_SortColumn = $this->m_int_COL_Tag;
			}
			$this->clear_Query();
			$this->add_ColumnQuery($this->m_int_COL_GroupID, $str_GroupID);
			$this->add_Sort($int_SortColumn, $str_SortColumn);

			if ($this->do_Search() > 0)
			{
				$arr_RS = $this->get_ResultSet();
				foreach ($arr_RS as $obj_RS)
				{
					$this->load_DataFromArray($obj_RS);
					$arr_Output[] = $this->output_AsArray();
				}
			}

			return $arr_Output;
		}

		public function add_Tag($str_GroupID, $str_Tag, $str_UserName)
		{
			$this->load_DataFromGroupTag($str_GroupID, $str_Tag);
			$obj_DateTime = new DateTime();

			$this->set_group_id($str_GroupID);
			$this->set_tag($str_Tag);
			$this->set_creator($str_UserName);
			$this->set_date($obj_DateTime->format("Y-M-d H:i:s"));

			$this->save_Data2DB();

		}

		public function delete_Tag($str_GroupID, $str_Tag)
		{
			$this->clear_Query();
			$this->add_ColumnQuery($this->m_int_COL_GroupID, $str_GroupID);
			$this->add_ColumnQuery($this->m_int_COL_Tag, $str_Tag);
			return $this->do_Delete();
		}

		public function load_DataFromGroupTag($str_GroupID, $str_Tag)
		{
			$this->clear();
			if (!isset($str_GroupID) || empty($str_GroupID))
			{
				return false;
			}
			if (!isset($str_Tag) || empty($str_Tag))
			{
				return false;
			}
			$this->clear_Query();
			$this->add_ColumnQuery($this->m_int_COL_GroupID, $str_GroupID);
			$this->add_ColumnQuery($this->m_int_COL_Tag, $str_Tag);

			if ($this->do_Search() > 0)
			{
				$obj_RS = $this->get_ResultSet();
				if ($obj_RS[0])
				{
					$this->load_DataFromArray($obj_RS[0]);
					return true;
				}
			}
			return false;
		}

	}
?>
